# 1. 导入 WinBotMain 类
from AiBot import WinBotMain
import os
import time
import threading


# 2. 自定义一个脚本类，继承 WinBotMain
class CustomWinScript(WinBotMain):
    choose=-1
    runned=-1
    # 3. 设置等待参数
    # 3.1 设置等待时间
    wait_timeout = 3
    # 3.2 设置重试间隔时长
    interval_timeout = 0.5

    # 4. 设置日志等级
    log_level = "INFO"  # "DEBUG"

    # 5. 设置方法超时是否抛出异常
    raise_err = False  # True
    def sclick(self,result,image,image_name):
        a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\{0}".format(image))
        if len(a)>0:
            self.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(1)
            return True
        else:
            print("没有找到%s的图"%image_name)
            time.sleep(1)
            return False
        
    def saodang(self,result):
        self.runned=self.choose
        self.choose=-1
        self.click_mouse(result,227,795,1,True)#点击副本
        time.sleep(1)
        self.click_mouse(result,228,453,1,True)#点击竞技对战
        time.sleep(1)
        self.click_mouse(result,228,233,1,True)#点击矿点争夺战
        time.sleep(1)
        i=1
        while True:
            print("========================第%d次扫荡========================"%i)
            self.sclick(result, "sd.png","扫荡")#点击扫荡
            time.sleep(2)            
            click_success=self.sclick(result, "sblq.png","双倍领取")  #点击双倍领取
            if not click_success:
                #判断是不是扫荡完毕
                a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sd.png")
                if len(a)>0:
                    break
                #判断是不是需要领取体力
                a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\lqtl.png")
                if len(a)>0:
                    self.click_mouse(result,230,573,1,True)#点击确定领取体力
                    print("等待广告")
                    for j in range(35):
                        print(35-j)
                        time.sleep(1)
                    self.click_mouse(result,411,75,1,True)
                    continue
                #判断是不是要清理装备
                a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\qlzb.png",similarity=0.8)
                if len(a)>0:
                    self.click_mouse(result,162,503,1,True)#点击清理装备
                    time.sleep(1)
                    self.click_mouse(result,401,518,1,True)#点击快速出售
                    time.sleep(1)
                    self.click_mouse(result,223,525,1,True)#点击快速出售
                    time.sleep(1)
                    self.click_mouse(result,48,79,1,True)#点击返回
                    time.sleep(1)
                    continue
            time.sleep(1)          
            a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sblq.png")
            if len(a)>0:
                print("等待广告")
                gg=False
                miao=0
                while not gg:
                    print("等待时间%d秒"%miao)
                    miao +=10
                    time.sleep(10)
                    gg=self.sclick(result,"sblq.png","双倍领取")#点击双倍领取            
            print("等待广告")
            for j in range(35):
                print(35-j)
                time.sleep(1)
            self.click_mouse(result,411,75,1,True)
            i=i+1
            time.sleep(1)
        print("扫荡完毕！")
        self.click_mouse(result,48,79,1,True)#点击返回
        time.sleep(1)
        self.click_mouse(result,48,79,1,True)#点击返回
        time.sleep(1)

    def yb(self,result):
        self.runned=self.choose
        self.choose=-1
        self.click_mouse(result,404,788,1,True)
        time.sleep(1)
        for i in range(5):
            self.click_mouse(result,309,273,1,True)
            time.sleep(2)
            a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            if len(a)>0:
                print("元宝领取完毕")
                break
            else:
                print("等待广告")
                for j in range(35):
                    print(35-j)
                    time.sleep(1)
                self.click_mouse(result,411,75,1,True)
                time.sleep(1)
                self.click_mouse(result,226,641,1,True)
                time.sleep(1)
        for i in range(5):
            self.click_mouse(result,137,518,1,True)
            time.sleep(2)
            a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            if len(a)>0:
                print("秘籍领取完毕")
                break
            else:
                print("等待广告")
                for j in range(35):
                    print(35-j)
                    time.sleep(1)
                self.click_mouse(result,411,75,1,True)
                time.sleep(4)
                self.click_mouse(result,167,688,1,True)
                time.sleep(2)
                self.click_mouse(result,167,688,1,True)
                time.sleep(1)
        for i in range(5):
            self.click_mouse(result,140,758,1,True)
            time.sleep(2)
            a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            if len(a)>0:
                print("角色领取完毕")
                break
            else:
                print("等待广告")
                for j in range(35):
                    print(35-j)
                    time.sleep(1)
                self.click_mouse(result,411,75,1,True)
                time.sleep(4)
                self.click_mouse(result,167,688,1,True)
                time.sleep(2)
                self.click_mouse(result,167,688,1,True)
                time.sleep(1)
        print("领取完毕！")
        self.click_mouse(result,48,79,1,True)#点击返回
        time.sleep(1)

    def mrrw(self,result):
        print("todo")

    def wnq(self,result):
        self.click_mouse(result,413,324,1,True)#点击万能券
        time.sleep(1)                 
        self.click_mouse(result,302,277,1,True)#点击免费获取
        time.sleep(2)
        a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mfhqqd.png")
        while len(a)>0:
            self.click_mouse(result,226,577,1,True)#点击确定
            time.sleep(2)
            a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mfhqqd.png")
            miao=0
            while len(a)>0:
                print("等待时间%d秒"%miao)
                miao +=10
                time.sleep(10)
                a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mfhq.png")
            if miao>0:
                self.click_mouse(result,226,577,1,True)#点击确定
            print("等待广告")
            for j in range(35):
                print(35-j)
                time.sleep(1)
            self.click_mouse(result,234,643,1,True)
            a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mfhqqd.png")
        print("领取完毕！")
        

    def menu(self):#显示菜单
        while True:
            print()
            print()
            print("=====剑侠风云挂机小工具v2022.12.18.13.34=====")
            print("=========1.矿点扫荡=============")
            print("=========2.免费元宝秘籍和角色===")
            print("=========3.万能券=============")
            print("=========4.无限轮回塔=============")
            print("=========5.一键完成=============")
            print("=========6.重新启动=============")
            print("=========0.退出=================")
            print()
            self.choose=input("请输入对应功能的数字：")
            if self.choose=="0":
                return

    # 6. 重写方法，编写脚本
    # 注意：此方法是脚本执行入口
    def script_main(self):
        # 查询指定窗口句柄
        result = self.find_window(window_name="剑侠风云")
        m=threading.Thread(target=self.menu)#显示菜单
        m.start()
        while self.choose!="0":    
            try:         
                if result is None:
                    result = self.find_window(window_name="Program Manager")
                    a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\logo.png")
                    self.click_mouse(result, a[0].x, a[0].y, 7)
                    time.sleep(2)
                    result = self.find_window(window_name="剑侠风云")
                    if result is None:
                        self.choose=self.menu()
                        continue
                if self.choose=="1":
                    self.saodang(result)
                elif self.choose=="2":   
                    self.yb(result)  
                elif self.choose=="3":
                    self.wnq(result)    
                '''elif choose=="4":
                    lh(result)  
                elif choose=="5":
                    saodang(result)
                    yb(result)    
                    mrjl(result)
                elif choose=="6":
                    choose=-1
                    self.sclick(result,"gbct.png","关闭窗体")
                    result = self.find_window(window_name="Program Manager")
                    a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\logo.png")
                    self.click_mouse(result, a[0].x, a[0].y, 7)
                    time.sleep(2)
                    result = self.find_window(window_name="剑侠风云")
                    if result is None:
                        choose=menu()
                        continue'''
            except Exception as e:
                self.sclick(result,"gbct.png")
                result = self.find_window(window_name="Program Manager")
                a=self.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\logo.png")
                self.click_mouse(result, a[0].x, a[0].y, 7)
                time.sleep(2)
                result = self.find_window(window_name="剑侠风云")
                self.choose=self.runned

        

# 7. 执行脚本，Pycharm 中，直接右键执行
if __name__ == '__main__':
    # 启动脚本，监听 6666 号端口

    # local=True 时，是本地运行脚本，会自动启动 WindowsDriver.exe 驱动；
    # 在远端部署脚本时，请设置 local=False，手动启动 WindowsDriver.exe，启动 WindowsDriver.exe 时需指定远端 IP 或端口号；

    CustomWinScript.execute(6666, local=True)